package com.dong.router.api

import android.content.Context
import com.dong.router.api.constant.Constants
import com.dong.router.api.interfaces.IRouterRegister
import com.dong.router.api.utils.ClassUtils

/**
 * @description:
 * @author dong
 * @date 2022/2/25 17:57
 */
object RouterRegister {

    /**
     * 执行编译器生成的代码，注册路由
     */
    fun register(context: Context) {
        //1.查找api包所有类
        val fileNames = ClassUtils.getFileNameByPackageName(context, Constants.GENERATE_PACKAGE_NAME)
        for (className in fileNames) {
            val clazz = Class.forName(className)
            //2.查找实现了IModuleRegister接口的类
            for (interfaceParent in clazz.interfaces) {
                if (interfaceParent == IRouterRegister::class.java) {
                    //3.反射创建对象，调用register方法
                    (clazz.getConstructor().newInstance() as IRouterRegister).register()
                }
            }
        }
    }

}